package com.datang.petstore.service.impl;

import com.datang.petstore.entity.Microblog;
import com.datang.petstore.entity.MicroblogTransmitRecord;
import com.datang.petstore.exception.AcceptResultCodeException;
import com.datang.petstore.generatemapper.MicroblogTransmitRecordMapper;
import com.datang.petstore.resultpage.ResultCode;
import com.datang.petstore.service.MicroblogService;
import com.datang.petstore.service.MicroblogTransmitRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-11-04 14:04
 * @notify 转发接口
 * @version 1.0
 */
@Service
public class MicroblogTransmitRecordServiceImpl implements MicroblogTransmitRecordService {

    @Autowired
    private MicroblogTransmitRecordMapper microblogTransmitRecordMapper;

    @Autowired
    private MicroblogService microblogService;

    /*
     * @author 顶风少年
     * @date 2019/11/4 14:10
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param microblogTransmitRecord
     * @result void
     * @notify 复制原微博,制作转发,并添加转发日志
     * version 1.0
     */
    @Transactional
    public void addMicroblogTransmitRecord(MicroblogTransmitRecord microblogTransmitRecord, Microblog microblogr) {
        //判断微博是否不存在或者被删除
        Microblog microblog = microblogService.getMicroblog(microblogTransmitRecord.getMicroblogId());
        if (microblog == null || microblog.getDel().equals("1")) {
            throw new AcceptResultCodeException(ResultCode.error505);
        }

        if (microblogTransmitRecord.getUserId().equals(microblog.getUserId())){
            throw new AcceptResultCodeException(ResultCode.error508);
        }

        //更改原微博
        microblog.setTransmitNumber(microblog.getTransmitNumber() + 1);
        microblogService.updateMicroblog(microblog);


        //复制原微博,重置原微博属性
        microblog.setId(null);
        microblog.setShareNumber(0);
        microblog.setInformNumber(0);
        microblog.setTransmitNumber(0);
        microblog.setLikeNumber(0);
        microblog.setCommentNumber(0);
        //保存被复制的微博的发布人id
        String microblogUserId = microblog.getUserId();
        microblog.setUserId(microblogTransmitRecord.getUserId());
        microblog.setDate(new Date());
        microblog.setDel("0");
        microblog.setOriginalId(microblog.getId());
        microblog.setTransmitText(microblogr.getTransmitText());
        microblogr.setFacilityModel(microblogr.getFacilityModel());
        microblogr.setCountry(microblogr.getCountry());
        microblogr.setProvince(microblogr.getProvince());
        microblogr.setCity(microblogr.getCity());
        microblogr.setDistrict(microblogr.getDistrict());
        microblogr.setAddress(microblogr.getAddress());
        //添加复制的微博
        Integer integer = microblogService.addMicroblog(microblog);

        //添加记录
        microblogTransmitRecord.setDate(new Date());
        //设置原发布人
        microblogTransmitRecord.setMicroblogUserId(microblogUserId);
        microblogTransmitRecord.setNewMicroblogId(integer);
        microblogTransmitRecordMapper.insertSelective(microblogTransmitRecord);


    }
}
